<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>类别列表</span>
      <el-button type="primary" @click="addDialog = true">添加分类</el-button>
    </div>
    <!-- 添加的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialog" :before-close="cancel" width="500px">
      <el-form label-width="80px" ref="addForm" :model="addData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑的对话框 -->
    <el-dialog title="添加分类" :visible.sync="editDialog" width="500px">
      <el-form label-width="80px" ref="editForm" :model="editData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="editData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="editData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateList">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 内容区，放表格 -->
    <el-table style="width: 100%" :data="list" stripe border>
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
      <el-table-column label="操作">
        <template v-slot="obj">
          <el-button size="mini" @click="showEditDialog(obj.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(obj.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { getListAPI, addListAPI, delIdAPI, updateListAPI } from '@/api/category'
export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      // 添加弹框的控制数据
      addDialog: false,
      // 编辑弹框的控制数据
      editDialog: false,
      // 编辑对话框
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑对话框
      editData: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          // {1,15} 不能写成 {1, 15}
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // 生命周期函数，自调用
  created() {
    this.getList()
  },
  methods: {
    // 获取
    async getList() {
      const { data: res } = await getListAPI()
      if (res.code === 0) {
        // 自动到渲染页面
        this.list = res.data
      }
    },
    // 添加
    addList() {
      this.$refs.addForm.validate(async val => {
        if (!val) return
        // console.log(val)
        const { data: res } = await addListAPI(this.addData)
        if (res.code === 0) {
          // 提示添加成功
          this.$message.success(res.message)
          // 重置表单
          this.$refs.addForm.resetFields()
          // 关闭弹框
          this.addDialog = false
          // 重新渲染页面
          this.getList()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      // 重置表单
      this.$refs.addForm.resetFields()
      // 关闭弹框
      this.addDialog = false
    },
    // 删除
    del(id) {
      this.$confirm('确定删除吗？', '提示', {
        type: 'warning',
        callback: async r => {
          // console.log(r) // r 的值可能是 confirm、cancel、close
          if (r === 'confirm') {
            const { data: res } = await delIdAPI(id)
            if (res.code === 0) {
              // 提示删除成功
              this.$message.success(res.message)
              // 渲染页面
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 显示编辑框
    showEditDialog(row) {
      // 浅拷贝
      this.editData = { ...row }
      this.editDialog = true
    },
    // 修改
    updateList() {
      this.$refs.editForm.validate(async val => {
        if (!val) return
        const { data: res } = await updateListAPI(this.editData)
        if (res.code === 0) {
          // 提示修改成功
          this.$message.success(res.message)
          // 重新渲染页面
          this.getList()
          // 关闭弹框
          this.editDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
